home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / rogue / room.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-13  |  8.4 KB  |  425 lines

  1. /*
  2.  * room.c
  3.  *
  4.  * This source herein may be modified and/or distributed by anybody who
  5.  * so desires, with the following restrictions:
  6.  *    1.)  No portion of this notice shall be removed.
  7.  *    2.)  Credit shall not be taken for the creation of this source.
  8.  *    3.)  This code is not to be traded, sold, or used for personal
  9.  *         gain or profit.
  10.  *
  11.  */
  12.  
  13. #ifndef CURSES
  14. #include <curses.h>
  15. #endif CURSES
  16. #include "rogue.h"
  17.  
  18. room rooms[MAXROOMS];
  19. boolean rooms_visited[MAXROOMS];
  20.  
  21. extern short blind;
  22. extern boolean detect_monster;
  23.  
  24. light_up_room(rn)
  25. int rn;
  26. {
  27.     short i, j;
  28.  
  29.     if (!blind) {
  30.         for (i = rooms[rn].top_row;
  31.             i <= rooms[rn].bottom_row; i++) {
  32.             for (j = rooms[rn].left_col;
  33.                 j <= rooms[rn].right_col; j++) {
  34.                 if (dungeon[i][j] & MONSTER) {
  35.                     object *monster;
  36.  
  37.                     if (monster = object_at(&level_monsters, i, j)) {
  38.                         dungeon[monster->row][monster->col] &= (~MONSTER);
  39.                         monster->trail_char =
  40.                             get_dungeon_char(monster->row, monster->col);
  41.                         dungeon[monster->row][monster->col] |= MONSTER;
  42.                     }
  43.                 }
  44.                 mvaddch(i, j, get_dungeon_char(i, j));
  45.             }
  46.         }
  47.         mvaddch(rogue.row, rogue.col, rogue.fchar);
  48.     }
  49. }
  50.  
  51. light_passage(row, col)
  52. {
  53.     short i, j, i_end, j_end;
  54.  
  55.     if (blind) {
  56.         return;
  57.     }
  58.     i_end = (row < (DROWS-2)) ? 1 : 0;
  59.     j_end = (col < (DCOLS-1)) ? 1 : 0;
  60.  
  61.     for (i = ((row > MIN_ROW) ? -1 : 0); i <= i_end; i++) {
  62.         for (j = ((col > 0) ? -1 : 0); j <= j_end; j++) {
  63.             if (can_move(row, col, row+i, col+j)) {
  64.                 mvaddch(row+i, col+j, get_dungeon_char(row+i, col+j));
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. darken_room(rn)
  71. short rn;
  72. {
  73.     short i, j;
  74.  
  75.     for (i = rooms[rn].top_row + 1; i < rooms[rn].bottom_row; i++) {
  76.         for (j = rooms[rn].left_col + 1; j < rooms[rn].right_col; j++) {
  77.             if (blind) {
  78.                 mvaddch(i, j, ' ');
  79.             } else {
  80.                 if (!(dungeon[i][j] & (OBJECT | STAIRS)) &&
  81.                     !(detect_monster && (dungeon[i][j] & MONSTER))) {
  82.                     if (!imitating(i, j)) {
  83.                         mvaddch(i, j, ' ');
  84.                     }
  85.                     if ((dungeon[i][j] & TRAP) && (!(dungeon[i][j] & HIDDEN))) {
  86.                         mvaddch(i, j, '^');
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. get_dungeon_char(row, col)
  95. register row, col;
  96. {
  97.     register unsigned short mask = dungeon[row][col];
  98.  
  99.     if (mask & MONSTER) {
  100.         return(gmc_row_col(row, col));
  101.     }
  102.     if (mask & OBJECT) {
  103.         object *obj;
  104.  
  105.         obj = object_at(&level_objects, row, col);
  106.         return(get_mask_char(obj->what_is));
  107.     }
  108.     if (mask & (TUNNEL | STAIRS | HORWALL | VERTWALL | FLOOR | DOOR)) {
  109.         if ((mask & (TUNNEL| STAIRS)) && (!(mask & HIDDEN))) {
  110.             return(((mask & STAIRS) ? '%' : '#'));
  111.         }
  112.         if (mask & HORWALL) {
  113.             return('-');
  114.         }
  115.         if (mask & VERTWALL) {
  116.             return('|');
  117.         }
  118.         if (mask & FLOOR) {
  119.             if (mask & TRAP) {
  120.                 if (!(dungeon[row][col] & HIDDEN)) {
  121.                     return('^');
  122.                 }
  123.             }
  124.             return('.');
  125.         }
  126.         if (mask & DOOR) {
  127.             if (mask & HIDDEN) {
  128.                 if (((col > 0) && (dungeon[row][col-1] & HORWALL)) ||
  129.                     ((col < (DCOLS-1)) && (dungeon[row][col+1] & HORWALL))) {
  130.                     return('-');
  131.                 } else {
  132.                     return('|');
  133.                 }
  134.             } else {
  135.                 return('+');
  136.             }
  137.         }
  138.     }
  139.     return(' ');
  140. }
  141.  
  142. get_mask_char(mask)
  143. register unsigned short mask;
  144. {
  145.         switch(mask) {
  146.         case SCROLL:
  147.             return('?');
  148.         case POTION:
  149.             return('!');
  150.         case GOLD:
  151.             return('*');
  152.         case FOOD:
  153.             return(':');
  154.         case WAND:
  155.             return('/');
  156.         case ARMOR:
  157.             return(']');
  158.         case WEAPON:
  159.             return(')');
  160.         case RING:
  161.             return('=');
  162.         case AMULET:
  163.             return(',');
  164.         default:
  165.             return('~');    /* unknown, something is wrong */
  166.         }
  167. }
  168.  
  169. gr_row_col(row, col, mask)
  170. short *row, *col;
  171. unsigned short mask;
  172. {
  173.     short rn;
  174.     short r, c;
  175.  
  176.     do {
  177.         r = get_rand(MIN_ROW, DROWS-2);
  178.         c = get_rand(0, DCOLS-1);
  179.         rn = get_room_number(r, c);
  180.     } while ((rn == NO_ROOM) ||
  181.         (!(dungeon[r][c] & mask)) ||
  182.         (dungeon[r][c] & (~mask)) ||
  183.         (!(rooms[rn].is_room & (R_ROOM | R_MAZE))) ||
  184.         ((r == rogue.row) && (c == rogue.col)));
  185.  
  186.     *row = r;
  187.     *col = c;
  188. }
  189.  
  190. gr_room()
  191. {
  192.     short i;
  193.  
  194.     do {
  195.         i = get_rand(0, MAXROOMS-1);
  196.     } while (!(rooms[i].is_room & (R_ROOM | R_MAZE)));
  197.  
  198.     return(i);
  199. }
  200.  
  201. party_objects(rn)
  202. {
  203.     short i, j, nf = 0;
  204.     object *obj;
  205.     short n, N, row, col;
  206.     boolean found;
  207.  
  208.     N = ((rooms[rn].bottom_row - rooms[rn].top_row) - 1) *
  209.         ((rooms[rn].right_col - rooms[rn].left_col) - 1);
  210.     n =  get_rand(5, 10);
  211.     if (n > N) {
  212.         n = N - 2;
  213.     }
  214.     for (i = 0; i < n; i++) {
  215.         for (j = found = 0; ((!found) && (j < 250)); j++) {
  216.             row = get_rand(rooms[rn].top_row+1,
  217.                        rooms[rn].bottom_row-1);
  218.             col = get_rand(rooms[rn].left_col+1,
  219.                        rooms[rn].right_col-1);
  220.             if ((dungeon[row][col] == FLOOR) || (dungeon[row][col] == TUNNEL)) {
  221.                 found = 1;
  222.             }
  223.         }
  224.         if (found) {
  225.             obj = gr_object();
  226.             place_at(obj, row, col);
  227.             nf++;
  228.         }
  229.     }
  230.     return(nf);
  231. }
  232.  
  233. get_room_number(row, col)
  234. register row, col;
  235. {
  236.     short i;
  237.  
  238.     for (i = 0; i < MAXROOMS; i++) {
  239.         if ((row >= rooms[i].top_row) && (row <= rooms[i].bottom_row) &&
  240.             (col >= rooms[i].left_col) && (col <= rooms[i].right_col)) {
  241.             return(i);
  242.         }
  243.     }
  244.     return(NO_ROOM);
  245. }
  246.  
  247. is_all_connected()
  248. {
  249.     short i, starting_room;
  250.  
  251.     for (i = 0; i < MAXROOMS; i++) {
  252.         rooms_visited[i] = 0;
  253.         if (rooms[i].is_room & (R_ROOM | R_MAZE)) {
  254.             starting_room = i;
  255.         }
  256.     }
  257.  
  258.     visit_rooms(starting_room);
  259.  
  260.     for (i = 0; i < MAXROOMS; i++) {
  261.         if ((rooms[i].is_room & (R_ROOM | R_MAZE)) && (!rooms_visited[i])) {
  262.             return(0);
  263.         }
  264.     }
  265.     return(1);
  266. }
  267.  
  268. visit_rooms(rn)
  269. int rn;
  270. {
  271.     short i;
  272.     short oth_rn;
  273.  
  274.     rooms_visited[rn] = 1;
  275.  
  276.     for (i = 0; i < 4; i++) {
  277.         oth_rn = rooms[rn].doors[i].oth_room;
  278.         if ((oth_rn >= 0) && (!rooms_visited[oth_rn])) {
  279.             visit_rooms(oth_rn);
  280.         }
  281.     }
  282. }
  283.  
  284. draw_magic_map()
  285. {
  286.     short i, j, ch, och;
  287.     unsigned short mask = (HORWALL | VERTWALL | DOOR | TUNNEL | TRAP | STAIRS |
  288.             MONSTER);
  289.     unsigned short s;
  290.  
  291.     for (i = 0; i < DROWS; i++) {
  292.         for (j = 0; j < DCOLS; j++) {
  293.             s = dungeon[i][j];
  294.             if (s & mask) {
  295.                 if (((ch = mvinch(i, j)) == ' ') ||
  296.                     ((ch >= 'A') && (ch <= 'Z')) || (s & (TRAP | HIDDEN))) {
  297.                     och = ch;
  298.                     dungeon[i][j] &= (~HIDDEN);
  299.                     if (s & HORWALL) {
  300.                         ch = '-';
  301.                     } else if (s & VERTWALL) {
  302.                         ch = '|';
  303.                     } else if (s & DOOR) {
  304.                         ch = '+';
  305.                     } else if (s & TRAP) {
  306.                         ch = '^';
  307.                     } else if (s & STAIRS) {
  308.                         ch = '%';
  309.                     } else if (s & TUNNEL) {
  310.                         ch = '#';
  311.                     } else {
  312.                         continue;
  313.                     }
  314.                     if ((!(s & MONSTER)) || (och == ' ')) {
  315.                         addch(ch);
  316.                     }
  317.                     if (s & MONSTER) {
  318.                         object *monster;
  319.  
  320.                         if (monster = object_at(&level_monsters, i, j)) {
  321.                             monster->trail_char = ch;
  322.                         }
  323.                     }
  324.                 }
  325.             }
  326.         }
  327.     }
  328. }
  329.  
  330. dr_course(monster, entering, row, col)
  331. object *monster;
  332. boolean entering;
  333. short row, col;
  334. {
  335.     short i, j, k, rn;
  336.     short r, rr;
  337.  
  338.     monster->row = row;
  339.     monster->col = col;
  340.  
  341.     if (mon_sees(monster, rogue.row, rogue.col)) {
  342.         monster->trow = NO_ROOM;
  343.         return;
  344.     }
  345.     rn = get_room_number(row, col);
  346.  
  347.     if (entering) {        /* entering room */
  348.         /* look for door to some other room */
  349.         r = get_rand(0, MAXROOMS-1);
  350.         for (i = 0; i < MAXROOMS; i++) {
  351.             rr = (r + i) % MAXROOMS;
  352.             if ((!(rooms[rr].is_room & (R_ROOM | R_MAZE))) || (rr == rn)) {
  353.                 continue;
  354.             }
  355.             for (k = 0; k < 4; k++) {
  356.                 if (rooms[rr].doors[k].oth_room == rn) {
  357.                     monster->trow = rooms[rr].doors[k].oth_row;
  358.                     monster->tcol = rooms[rr].doors[k].oth_col;
  359.                     if ((monster->trow == row) &&
  360.                         (monster->tcol == col)) {
  361.                         continue;
  362.                     }
  363.                     return;
  364.                 }
  365.             }
  366.         }
  367.         /* look for door to dead end */
  368.         for (i = rooms[rn].top_row; i <= rooms[rn].bottom_row; i++) {
  369.             for (j = rooms[rn].left_col; j <= rooms[rn].right_col; j++) {
  370.                 if ((i != monster->row) && (j != monster->col) &&
  371.                     (dungeon[i][j] & DOOR)) {
  372.                     monster->trow = i;
  373.                     monster->tcol = j;
  374.                     return;
  375.                 }
  376.             }
  377.         }
  378.         /* return monster to room that he came from */
  379.         for (i = 0; i < MAXROOMS; i++) {
  380.             for (j = 0; j < 4; j++) {
  381.                 if (rooms[i].doors[j].oth_room == rn) {
  382.                     for (k = 0; k < 4; k++) {
  383.                         if (rooms[rn].doors[k].oth_room == i) {
  384.                             monster->trow = rooms[rn].doors[k].oth_row;
  385.                             monster->tcol = rooms[rn].doors[k].oth_col;
  386.                             return;
  387.                         }
  388.                     }
  389.                 }
  390.             }
  391.         }
  392.         /* no place to send monster */
  393.         monster->trow = -1;
  394.     } else {        /* exiting room */
  395.         if (!get_oth_room(rn, &row, &col)) {
  396.             monster->trow = NO_ROOM;
  397.         } else {
  398.             monster->trow = row;
  399.             monster->tcol = col;
  400.         }
  401.     }
  402. }
  403.  
  404. get_oth_room(rn, row, col)
  405. short rn, *row, *col;
  406. {
  407.     short d = -1;
  408.  
  409.     if (*row == rooms[rn].top_row) {
  410.         d = UP/2;
  411.     } else if (*row == rooms[rn].bottom_row) {
  412.         d = DOWN/2;
  413.     } else if (*col == rooms[rn].left_col) {
  414.         d = LEFT/2;
  415.     } else if (*col == rooms[rn].right_col) {
  416.         d = RIGHT/2;
  417.     }
  418.     if ((d != -1) && (rooms[rn].doors[d].oth_room >= 0)) {
  419.         *row = rooms[rn].doors[d].oth_row;
  420.         *col = rooms[rn].doors[d].oth_col;
  421.         return(1);
  422.     }
  423.     return(0);
  424. }
  425.